#include "mainwindow.h"
#include <QKeyEvent>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setGeometry(100,50,800,600);

    //snake活动范围捏
    int area_x = 100;
    int area_y = 50;
    int area_h = 500;
    int area_w = 600;

    //步长与速度值。
    int step = 20;
    int speed =100;


    //显示snake在主窗口的活动区域
    area = new showType(this);
    area->setStyleSheet(AREA_STYLE);
    area->setGeometry((area_x/step)*step,(area_y/step)*step,(area_w/step)*step,(area_h/step)*step);


    //初始化sanke捏
    snake = new snakeType;
    //初始长度
    int init_size = 3;
    for(int i = 0 ; i < init_size ; i++)
    {
        //snake 所有坐标必须是step的整数倍捏
        snakeNodePtr snakenode = new snakeNode;
        showtype = new showType(this);
        snakenode->x = (area_x/step)*step+i*step;
        snakenode->y = (area_y/step)*step;
        snakenode->step = step;
        snakenode->style = BOYD_STYLE;
        snakenode->type = snakeNode::body;
        snakenode->disp = showtype;

        if(i == 0)
        {
            snakenode->type = snakeNode::tail;
            snakenode->style = TAIL_STYLE;

        }
        if(i == (init_size-1))
        {
            snakenode->type = snakeNode::head;
            snakenode->style = HEAD_STYLE;
        }

        snakenode->disp->setStyleSheet(snakenode->style);
        snakenode->disp->setGeometry(snakenode->x,snakenode->y,snakenode->step,snakenode->step);

        snake->prepend(snakenode);
    }

    //创建sanke操作对象捏
    playsnake = new PlaySnake(this);
    //连接接受playsnake发出的信号和处理的槽函数
    connect(playsnake,&PlaySnake::updataStyle,this,&MainWindow::updataStyle);
    connect(playsnake,&PlaySnake::updataNode,this,&MainWindow::updataNode);
    connect(playsnake,&PlaySnake::eaten,this,&MainWindow::updataFrog);
    connect(playsnake,&PlaySnake::snakeMsg,this,&MainWindow::dealSnakeMsg);

    playsnake->setSpeed(speed);
    playsnake->setStep(step);
    playsnake->setRect(area_w,area_h,area_x,area_y);
    playsnake->setSnake(snake);
    playsnake->start();


}

//释放内存（释放自我！）
MainWindow::~MainWindow()
{
    playsnake->stop();
    playsnake->wait();

    if(snake)
    {
        foreach (snakeNodePtr var, *snake) {
            delete var->disp;
            var->disp = nullptr;
            delete var;
            var = nullptr;
        }

        delete snake;
        snake = nullptr;
    }

    delete playsnake;
    delete area;

    area = nullptr;
    playsnake = nullptr;
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
       static bool pause = false;

      switch (event->key()) {
        case Qt::Key_U :
            playsnake->setSpeedFlag(true);
            break;
        case Qt::Key_W :
        case Qt::Key_Up :
             playsnake->setDirection(PlaySnake::Up);
             break;
        case Qt::Key_S :
        case Qt::Key_Down :
            playsnake->setDirection(PlaySnake::Down);
            break;
        case Qt::Key_D :
        case Qt::Key_Right :
            playsnake->setDirection(PlaySnake::Right);
            break;
        case Qt::Key_A :
        case Qt::Key_Left :
            playsnake->setDirection(PlaySnake::Left);
            break;
        case Qt::Key_P :
            pause = !pause;
            playsnake->setpause(pause);
            break;
      }
}

void MainWindow::keyReleaseEvent(QKeyEvent *event)
{
    if(event->key() == Qt::Key_U)
    {
        playsnake->setSpeedFlag(false);
    }
}

void MainWindow::updataStyle(showType *lab, QString style)
{
    lab->setStyleSheet(style);
    lab->raise();
    this->update();
}

void MainWindow::updataNode(showType *lab, QString style,int x,int y ,int step)
{
    lab->setStyleSheet(style);
    lab->setGeometry(x,y,step,step);
    lab->raise();
    this->update();
}

void MainWindow::updataFrog(int x, int y,int step)
{
    QString style =FROG_STYLE1;
    snakeNodePtr frog = new snakeNode;
    frog->x = x;
    frog->y = y;
    frog->disp = new showType(this);
    frog->style = style;
    frog->step = step;

    frog->type = snakeNode::head;

    frog->disp->setGeometry(frog->x,frog->y,frog->step,frog->step);
    frog->disp->setStyleSheet(frog->style);
    frog->disp->show();

    playsnake->setFrog(frog);
}
//终于快完了
void MainWindow::dealSnakeMsg(int code)
{
    switch (code) {
    case IS_WALL:
        qDebug()<<"IS_WALL";
        break;
    case IS_BODY:
        qDebug()<<"IS_BODY";
        break;
    case FULL_AREA:
        playsnake->stop();
        playsnake->wait();
        qDebug()<<"FULL_AREA";
        break;
    case EATEN:
        qDebug()<<"EATEN";
        break;
    default:
        break;
    }
}
//太拉了